package BusinessService;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import DataAccess.DAOAccount;
import DataAccess.DAOContact_information;
import DataAccess.DAOCustomer;
import Domain.Account;
import Domain.Customer;
import Exception.DadosException;

public class AccountControl {
	
	DAOAccount daoAccount;
	DAOCustomer daoCustomer;
	DAOContact_information daoContact;
	
	
	public AccountControl() {
		
		daoAccount = new DAOAccount();
		daoCustomer = new DAOCustomer();
		daoContact = new DAOContact_information();	
		
	}
	
	
	
	public void insert(Account ac,String senha2) throws DadosException{
		
			
		if(daoAccount.usernameValid(ac) == false){
			throw new DadosException(DadosException.ERROR_USERNAMENOTVALID);
		}
		
		System.out.println(ac.getPassword());
		System.out.println(senha2);
		if(!ac.getPassword().equals(senha2)){
			
			throw new DadosException(DadosException.ERROR_PASSWORD);
		}
		
			ac.getCustomer().setType(1);
		
			daoCustomer.inserir(ac.getCustomer());			
			daoContact.inserir(ac.getContact_information());
			daoAccount.inserir(ac);
			
	
		
	}
	
	public Account logar(String username, String password) throws DadosException{
		
		Account account = daoAccount.login(username, password);
		
		if(account == null){
			throw new DadosException(DadosException.ERROR_LOGIN_INVALIDO);			
		}
		
		return account;
		
	}
	
	
	
}
